/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pantallas;

import clases.Pieza;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author DanielRH
 */
public class PantallaNuevaPieza extends javax.swing.JDialog {

    /**
     * Creates new form PantallaNuevaPieza
     */
    public PantallaNuevaPieza(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        txtCantidad.addKeyListener(new KeyAdapter()
        {
            @Override
        public void keyTyped(KeyEvent e)
        {
            char caracter = e.getKeyChar();
            // Verificar si la tecla pulsada no es un digito
            if(((caracter < '0') ||
                (caracter > '9')) &&
                (caracter != '\b' /*corresponde a BACK_SPACE*/))
            {
                e.consume();  // ignorar el evento de teclado
            }
        }
        });
        
        txtAncho.addKeyListener(new KeyAdapter()
        {
            @Override
        public void keyTyped(KeyEvent e)
        {
            char caracter = e.getKeyChar();
            // Verificar si la tecla pulsada no es un digito
            if(((caracter < '0') ||
                (caracter > '9')) &&
                (caracter != '\b' /*corresponde a BACK_SPACE*/))
            {
                e.consume();  // ignorar el evento de teclado
            }
            else
            {
//                hacerCubo();
            }
        }
        });
        txtLargo.addKeyListener(new KeyAdapter()
        {
            @Override
        public void keyTyped(KeyEvent e)
        {
            char caracter = e.getKeyChar();
            // Verificar si la tecla pulsada no es un digito
            if(((caracter < '0') ||
                (caracter > '9')) &&
                (caracter != '\b' /*corresponde a BACK_SPACE*/))
            {
                e.consume();  // ignorar el evento de teclado
            }
            else
            {
//                hacerCubo();
            }
        }
        });
        txtEspesor.addKeyListener(new KeyAdapter()
        {
            @Override
        public void keyTyped(KeyEvent e)
        {
            char caracter = e.getKeyChar();
            // Verificar si la tecla pulsada no es un digito
            if(((caracter < '0') ||
                (caracter > '9')) &&
                (caracter != '\b' /*corresponde a BACK_SPACE*/))
            {
                e.consume();  // ignorar el evento de teclado
            }
            else
            {
//                hacerCubo();
            }
        }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtNombrePieza = new javax.swing.JTextField();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblInformacion = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtLargo = new javax.swing.JTextField();
        txtAncho = new javax.swing.JTextField();
        txtEspesor = new javax.swing.JTextField();
        txtCantidad = new javax.swing.JTextField();
        panelDibujo = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Nombre de la Pieza:");

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lblInformacion.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setText("Largo (cm):");

        jLabel3.setText("Ancho (cm):");

        jLabel4.setText("Espesor (mm):");

        jLabel5.setText("Cantidad:");

        panelDibujo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        javax.swing.GroupLayout panelDibujoLayout = new javax.swing.GroupLayout(panelDibujo);
        panelDibujo.setLayout(panelDibujoLayout);
        panelDibujoLayout.setHorizontalGroup(
            panelDibujoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 285, Short.MAX_VALUE)
        );
        panelDibujoLayout.setVerticalGroup(
            panelDibujoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtEspesor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                            .addComponent(txtAncho, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtLargo, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombrePieza, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCantidad)))
                    .addComponent(lblInformacion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(panelDibujo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(panelDibujo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtNombrePieza, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtLargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtAncho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtEspesor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnAceptar)
                            .addComponent(btnCancelar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblInformacion, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:
        lblInformacion.setText("");
        
        if(chekearTextFields()==false)
        {
            JOptionPane.showMessageDialog(getContentPane(),"Debe ingresar todos los datos","Error",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            String nombre=txtNombrePieza.getText();
            String largo=txtLargo.getText();
            String ancho=txtAncho.getText();
            String espesor=txtEspesor.getText();
            String cantidad=txtCantidad.getText();
            Pieza nueva=new Pieza();
            nueva.setNombre(nombre);
            nueva.setLargoCM(Float.parseFloat(largo));
            nueva.setAnchoCM(Float.parseFloat(ancho));
            nueva.setEspesorMM(Integer.parseInt(espesor));
            nueva.setCantidad(Integer.parseInt(cantidad));
            PantallaPiezas padre=(PantallaPiezas)this.getParent();
            padre.agregarNuevaPieza(nueva);
            this.dispose();
        }
        
    }//GEN-LAST:event_btnAceptarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaNuevaPieza.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaNuevaPieza.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaNuevaPieza.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaNuevaPieza.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                PantallaNuevaPieza dialog = new PantallaNuevaPieza(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    
    private boolean chekearTextFields()
    {
        if(txtNombrePieza.getText().compareTo("")==0)
        {
            return false;
        }
        if(txtLargo.getText().compareTo("")==0)
        {
            return false;
        }
        if(txtAncho.getText().compareTo("")==0)
        {
            return false;
        }
        if(txtEspesor.getText().compareTo("")==0)
        {
            return false;
        }
        if(txtCantidad.getText().compareTo("")==0)
        {
            return false;
        }
        return true;
    }
    
    private void dibujar(int ancho1, int largo, int espesor)
    {
        int alto=espesor/10;
        int ancho=largo;
        int profundidad=ancho1/2;
        
        int valorX[]=new int[4];
        int valorY[]=new int[4];
        
        valorX[0]=40+ancho;
        valorY[0]=80;
        
        valorX[1]=40+ancho;
        valorY[1]=80+alto;
        
        valorX[2]=40;
        valorY[2]=80+alto;
        
        valorX[3]=40;
        valorY[3]=80;
        panelDibujo.getGraphics().drawPolygon(valorX, valorY, 4);
        
        valorX[0]=40+ancho;
        valorY[0]=80;
        
        valorX[1]=40+ancho;
        valorY[1]=80+alto;
        
        valorX[2]=40+ancho+profundidad;
        valorY[2]=80-profundidad+alto;
        
        valorX[3]=40+ancho+profundidad;
        valorY[3]=80-profundidad;
        
        panelDibujo.getGraphics().drawPolygon(valorX, valorY, 4);
        
        valorX[0]=40+ancho;
        valorY[0]=80;
        
        valorX[1]=40+ancho+profundidad;
        valorY[1]=80-profundidad;
        
        valorX[2]=40+profundidad;
        valorY[2]=80-profundidad;
        
        valorX[3]=40;
        valorY[3]=80;
        panelDibujo.getGraphics().drawPolygon(valorX, valorY, 4);
    }
    
    private void hacerCubo()
    {
//        panelDibujo.getGraphics()
        int largo=0;
        int ancho=0;
        int espesor=0;
        if(txtLargo.getText().compareTo("")!=0)
        {
            largo=Integer.parseInt(txtLargo.getText());
        }
        
        if(txtAncho.getText().compareTo("")!=0)
        {
            ancho=Integer.parseInt(txtAncho.getText());
        }
        
        if(txtEspesor.getText().compareTo("")!=0)
        {
            espesor=Integer.parseInt(txtEspesor.getText());
        }
        
        dibujar(ancho, largo, espesor);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lblInformacion;
    private javax.swing.JPanel panelDibujo;
    private javax.swing.JTextField txtAncho;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtEspesor;
    private javax.swing.JTextField txtLargo;
    private javax.swing.JTextField txtNombrePieza;
    // End of variables declaration//GEN-END:variables
}
